package com.l.web.service;

import org.l.qiao.*;
import org.l.qiao.base.LType;

/**
 * Created by 严秋旺 on 2015/1/18.
 */
public class KeyUpdate extends LBean {

    private Class objectClass;

    /**
     * id
     */
    @Key(keyType = KeyType.PropertyKey, valueType = LType.LTYPE_STRING, check = Validate.OBJECTID)
    private String id;

    /**
     * 键名
     */
    @Key(keyType = KeyType.PropertyKey, valueType = LType.LTYPE_STRING, check = "^\\w{1,20}$")
    private String name;

    /**
     * 值
     */
    @Key(keyType = KeyType.PropertyKey, valueType = LType.LTYPE_STRING, check = "")
    private String value;

    public KeyUpdate(Class clusterClass) {
        objectClass = clusterClass;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    /**
     * 获取键名
     *
     * @return
     */
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    /**
     * 获取值
     *
     * @return 返回原始值
     */
    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    /**
     * 验证原始值
     *
     * @return
     */
    public boolean check() {
        return id != null && ClusterUtil.checkValue(objectClass, name, value);
    }

    /**
     * 获取键值
     *
     * @return
     * @throws NoSuchFieldException
     * @throws IllegalAccessException
     */
    public String getKey() throws NoSuchFieldException, IllegalAccessException {
        return ClusterUtil.getKey(objectClass, name);
    }

    /**
     * 值转换
     *
     * @return 转换的值对象
     * @throws Exception
     */
    public Object parseValue() throws Exception {
        return ClusterUtil.parseValue(objectClass, name, value);
    }
}
